package com.jlboot.model.po.sys;


import com.jlboot.core.Results;
import com.jlboot.core.base.Models;
import com.jfinal.plugin.activerecord.Model;
import com.jlboot.core.consts.Consts;
import com.jlboot.core.plugins.cron4j.MyCron4jPlugin;
import com.jlboot.core.util.LoginUtil;

import java.util.Date;
import java.util.List;

/**
 * table: sys_task
 * author: wuyang
 * Date: 2022-03-29 15:08:49
 */
public class SysTask extends Model<SysTask> {
    public static final SysTask dao = new SysTask().dao();
    public static final String table="sys_task";
    public Models mod = Models.n(this);

    public static String getNextId(){
        return SysIdGenerator.dao.getNextId(table);
    }

    public SysTask setCommon(){
        Date now=new Date();
        this.set("id",SysIdGenerator.dao.getNextId(table));
        this.set("del_tag", Consts.COMMON_NONE_DEL);
        this.set("create_time",now);
        this.set("update_time",now);
        this.set("create_by", LoginUtil.getUserId());
        this.set("update_by",LoginUtil.getUserId());
        return this;
    }

    /**
     * @param taskId
     * @return
     */
    public Results startTask(String taskId) {
        MyCron4jPlugin.removeTask(taskId);
        return addTask(taskId);
    }

    /**
     * @param taskId
     * @return
     */
    public Results stopTask(String taskId) {
        boolean status = MyCron4jPlugin.stopTask(taskId);
        return Results.n(status ? 0 : 9, "计划任务已停止");
    }

    /**
     * 添加任务
     *
     * @param taskId
     * @return
     */
    public Results addTask(String taskId) {
        Results results = Results.n().setMsg("加载成功");

        SysTask task = SysTask.dao.findById(taskId);
        String id = task.get("id");
        String name = task.get("task_name");
        if (task != null) {
            try{
                String cron = task.mod.getStr("task_cron");
                String task_class = task.mod.getStr("task_class");
                Object taskObj = Class.forName(task_class).newInstance();
                MyCron4jPlugin.getPlugin().addTask(id,name, cron,taskObj, true, true, true);
            }catch (Exception e){
                e.printStackTrace();
                results.setCode(9).setMessage(e.getMessage());
            }
        }

        return results;
    }

    /**
     * 从数据库中读取初始化计划任务
     */
    public void initAllTask() {
        List<SysTask> list = SysTask.dao.findAll();
        for (SysTask task : list) {
            if (task.mod.getInt("is_auto", 1) == 1){
                addTask(task.get("id"));
            }
        }
    }


    /**
     * 关闭所有计划任务
     */
    public void closeAllTask() {
        List<SysTask> list = SysTask.dao.findAll();
        for (SysTask task : list) {
            stopTask(task.get("id"));
        }
    }

    /**
     * @param taskName
     */
    public void updateLastAction(String taskId) {
        try {
            SysTask task = SysTask.dao.findById(taskId);
            if (task != null) {
                task.set("last_done", new Date()).update();
                if (task.getInt("is_log") == 1) {
                    SysWorkLog worklog = new SysWorkLog();
                    worklog.setCommon();
                    worklog.set("log_result", 1).set("log_title", "执行成功").set("log_time", new Date());
                    worklog.set("log_ip", "127.0.0.1").set("log_os", "").set("log_browser", "").set("log_desc", "");
                    worklog.set("key1", taskId);
                    worklog.set("log_type", 98).set("id", SysWorkLog.getNextId()).set("user_name", "").set("login_id", "");

                    SysWorkLog.dao.saveLog(worklog);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * @param taskName
     */
    public void updateLastAction(String taskId, String errMessage) {
        try {
            SysTask task = SysTask.dao.findById(taskId);
            if (task != null) {
                task.set("last_done", new Date()).update();
                if (task.getInt("is_log") == 1) {
                    SysWorkLog worklog = new SysWorkLog();
                    worklog.setCommon();
                    worklog.set("log_result", 2).set("log_title", "执行失败").set("log_time", new Date());
                    worklog.set("log_ip", "127.0.0.1").set("log_os", "").set("log_browser", "").set("log_desc", errMessage);
                    worklog.set("key1", taskId);
                    worklog.set("log_type", 98).set("id", SysWorkLog.getNextId()).set("user_name", "").set("login_id", "");
                    SysWorkLog.dao.saveLog(worklog);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 现在就执行一次
     *
     * @param taskId
     */
    public Results runSysTask(String taskId) {
        MyCron4jPlugin.runTask(taskId);
        return Results.n().setCode(0).setMsg("执行成功");
    }

}